package com.kgc.skProvider.service.serviceimpl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.SalesCollectionRefund;
import com.kgc.collection.pojo.SalesCollectionRefundDetail;
import com.kgc.collection.pojo.SalesCollectiondetail;
import com.kgc.skProvider.dao.SalesCollectionRefundMapper;
import com.kgc.skProvider.service.SalesCollectionRefundService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SalesCollectionRefundServiceImpl implements SalesCollectionRefundService {

    private Logger logger= LoggerFactory.getLogger(SalesCollectionRefundServiceImpl.class);

    @Autowired
    private SalesCollectionRefundMapper salesCollectionRefundMapper;


    @Override
    public ResponseMsg selectSalesCollectionRefundByPage(Map<String, Object> map) {
        logger.info("SalesCollectionRefundServiceImpl selectSalesCollectionRefundByPage params is {}", map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        PageInfo<SalesCollectionRefund> pageInfo=new PageInfo<SalesCollectionRefund>();
        pageInfo.setPageNo(map.get("pageNo")==null?1:Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize")==null?10:Integer.parseInt(map.get("pageSize").toString()));
        Page page= PageHelper.startPage(pageInfo.getPageNo(),pageInfo.getPageSize());
        List<SalesCollectionRefund> list=salesCollectionRefundMapper.selectSalesCollectionRefundByPage(map);
        pageInfo.setList(list);
        pageInfo.setTotal((int)page.getTotal());
        responseMsg.setData(pageInfo);
        logger.debug("SalesCollectionRefundServiceImpl selectSalesCollectionRefundByPage is success");
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg insertSalesCollectionRefund(Map<String, Object> map) {
        logger.info("SalesCollectionRefundServiceImpl insertSalesCollectionRefund params is {}", map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        ObjectMapper mapper = new ObjectMapper();
        SalesCollectionRefund salesCollectionRefund=mapper.convertValue(map.get("salesCollectionRefund"), SalesCollectionRefund.class);
        List<?> rawDetails = (List<?>) map.get("details");
        List<SalesCollectionRefundDetail> details=mapper.convertValue(rawDetails,new TypeReference<List<SalesCollectionRefundDetail>>(){});
        int result1=salesCollectionRefundMapper.insertSalesCollectionRefund(salesCollectionRefund);
        try{
            for(SalesCollectionRefundDetail detail:details){
                salesCollectionRefundMapper.insertSalesCollectionRefundDetail(detail);
            }
            responseMsg.setCode("200");
        }catch (Exception e){
            e.printStackTrace();
            responseMsg.setCode("201");
            return responseMsg;
        }
        if(result1>0){
            responseMsg.setCode("200");
        }else{
            responseMsg.setCode("201");
        }
        logger.debug("SalesCollectionRefundServiceImpl insertSalesCollectionRefund is success");
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg updateSalesCollectionRefund(Map<String, Object> map) {
        logger.info("SalesCollectionRefundServiceImpl updateSalesCollectionRefund params is {}", map.toString());
        //删除旧数据
        deleteSalesCollectionRefund(map);
        //新增数据
        ResponseMsg responseMsg=insertSalesCollectionRefund(map);
        logger.debug("SalesCollectionRefundServiceImpl updateSalesCollectionRefund is success");
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg deleteSalesCollectionRefund(Map<String, Object> map) {
        logger.info("SalesCollectionRefundServiceImpl deleteSalesCollectionRefund params is {}", map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        int result1=salesCollectionRefundMapper.deleteSalesCollectionRefund(map.get("docCode").toString());
        int result2=salesCollectionRefundMapper.deleteSalesCollectionRefundDetail(map.get("docCode").toString());
        if(result1>0&&result2>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesCollectionRefundServiceImpl deleteSalesCollectionRefund is success");
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg checkSalesCollectionRefund(Map<String, Object> map) {
        logger.info("SalesCollectionRefundServiceImpl checkSalesCollectionRefund params is {}", map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        int result1=salesCollectionRefundMapper.checkSalesCollectionRefund(map);
        if(result1>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesCollectionRefundServiceImpl checkSalesCollectionRefund is success");
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesCollectionRefundByCode(Map<String, Object> map) {
        logger.info("SalesCollectionRefundServiceImpl selectSalesCollectionRefundByCode is {}", map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        SalesCollectionRefund salesCollectionRefund=salesCollectionRefundMapper.selectSalesCollectionRefundByCode(map.get("docCode").toString());
        if(salesCollectionRefund!=null){
            List<SalesCollectionRefundDetail> list=salesCollectionRefundMapper.selectSalesCollectionRefundDetailByCode(map.get("docCode").toString());
            salesCollectionRefund.setDetails(list);
            responseMsg.setCode("200");
            responseMsg.setData(salesCollectionRefund);
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesCollectionRefundServiceImpl selectSalesCollectionRefundByCode is success");
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesCollectionRefundDetailByCode(Map<String, Object> map) {
        logger.info("SalesCollectionRefundServiceImpl selectSalesCollectionRefundDetailByCode is {}", map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        List<SalesCollectionRefundDetail> list=salesCollectionRefundMapper.selectSalesCollectionRefundDetailByCode(map.get("docCode").toString());
        if(!list.isEmpty()){
            responseMsg.setCode("200");
            responseMsg.setData(list);
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesCollectionRefundServiceImpl selectSalesCollectionRefundDetailByCode is success");
        return responseMsg;
    }
}
